package Controllers

import (
	"encoding/json"
	"fmt"
	"gPublic/Common"
	"io"
	"net/http"
	"net/url"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ResponseData struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

var urlPath = "https://novnc-k8sb.eeeknow.com/dockerTest.php"

func UsersDirList(c *gin.Context) {
	dt := url.Values{}
	dt.Set("c", "getFileList")
	dt.Set("path", "/mnt/acs_mnt/nas/learndata/users")
	dt.Set("is_sub", strconv.FormatBool(true))

	r, err := http.PostForm(urlPath, dt)

	if err != nil {
		Common.ErrorResponse(c, Common.ERROR, err)
		return
	}

	defer r.Body.Close()

	contents := make([]byte, 0)
	if r.StatusCode == http.StatusOK {
		contents, err = io.ReadAll(r.Body)
		if err != nil {
			Common.ErrorResponse(c, Common.ERROR, err)
			return
		}
	}

	// 把返回的json转出来 【转为结构体】
	var responseData ResponseData
	err = json.Unmarshal(contents, &responseData)
	if err != nil {
		panic(err)
	}

	data := responseData.Data
	// fmt.Printf(" - --------------------- %#v:", data)
	fmt.Printf(" - -----------code---------- %#v:\n", responseData.Code)
	fmt.Printf(" - -----------msg---------- %#v:", responseData.Msg)

	Common.SuccessResponse(c, Common.SUCCESS, data)
}

// 获取目录大小
func UsersDirSize(c *gin.Context) {
	path := c.Query("path")
	dt := url.Values{}
	dt.Set("c", "getFileSize")
	dt.Set("path", path)

	r, err := http.PostForm(urlPath, dt)

	if err != nil {
		Common.ErrorResponse(c, Common.ERROR, err)
		return
	}

	defer r.Body.Close()

	contents := make([]byte, 0)
	if r.StatusCode == http.StatusOK {
		contents, err = io.ReadAll(r.Body)
		if err != nil {
			Common.ErrorResponse(c, Common.ERROR, err)
			return
		}
	}

	if len(contents) > 0 {
		// 把返回的json转出来 【转为结构体】
		var responseData ResponseData
		err = json.Unmarshal(contents, &responseData)
		if err != nil {
			panic(err)
		}

		data := responseData.Data
		fmt.Printf("data:%v\n code:%v:\n msg:%v\n", responseData.Data, responseData.Code, responseData.Msg)
		var size, result string = "", ""

		if val, ok := data.(float64); ok {
			if val > 0 {
				size = Common.BytesToMegabytes(int64(val))
				result = fmt.Sprintf("%s%s", size, "M")
			} else {
				size, result = "", ""
			}
		} else {
			size, result = "", ""
		}
		Common.SuccessResponse(c, Common.SUCCESS, result)
		return
	}
	Common.SuccessResponse(c, Common.SUCCESS, "")
}

// 删除目录
func UsersDirDel(c *gin.Context) {
	dt := url.Values{}
	dt.Set("c", "removeFile")
	dt.Set("path", "/mnt/acs_mnt/nas/learndata/users")

	r, err := http.PostForm(urlPath, dt)

	if err != nil {
		Common.ErrorResponse(c, Common.ERROR, err)
		return
	}

	defer r.Body.Close()

	contents := make([]byte, 0)
	if r.StatusCode == http.StatusOK {
		contents, err = io.ReadAll(r.Body)
		if err != nil {
			Common.ErrorResponse(c, Common.ERROR, err)
			return
		}
	}

	// 把返回的json转出来 【转为结构体】
	var responseData ResponseData
	err = json.Unmarshal(contents, &responseData)
	if err != nil {
		panic(err)
	}
	// 'code' => 1, 'msg' => 'success', 'data' => 1
	data := responseData.Data

	if responseData.Code == 1 && responseData.Msg == "success" && responseData.Data == 1 {
		Common.SuccessResponse(c, Common.SUCCESS, data)
		return
	}
	Common.ErrorResponse(c, Common.ERROR, responseData.Msg)
}
